package org.xgluo.config;

import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import org.xgluo.Constant;
import org.xgluo.excle.ExcleConstant;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author xgluo
 * @program: JBOToExcle
 * @name: XmlConfiguration
 * @description: 读取Xml文件的文件相关属性配制
 * @date 2025年03月14日
 */
public class XmlConfiguration {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    // 指定Sheet页的方式，是所有节点还是特定的重复节点（需要指定节点名称）
    private static String sheetNodeType;
    // 指定Sheet页的节点名称（重复节点，每个节点作为一个sheet页时使用）
    private static String sheetNodeName;
    // 指定Sheet页名称的属性（重复节点，每个节点作为一个sheet页时使用）
    private static String sheetNameAttribute;
    // 指定sheet的描述信息属性
    private static String sheetDescriptionAttribute;

    private static String rowNodeName;
    private static String rowNodeType;
    private static int rowOffset;
    private static int cellOffset;

    private static Map<String,String> attributeNameMap;

    static {
        new XmlConfiguration();
    }
    private XmlConfiguration() {
        //定义一个解析工具
        Properties properties = new Properties();
        InputStream inputStream;
        try {
            inputStream = XmlConfiguration.class.getClassLoader().getResourceAsStream(Constant.XML_FORMAT_PROPERTIES_PATH);
            properties.load(inputStream);

            // 读取配置参数
            String sheetNodeType = properties.getProperty("sheetNameType", ExcleConstant.SHEET_NAME_TYPE_REPEAT);
            String sheetNodeAttribute = properties.getProperty("sheetNameAttribute");
            String sheetNodeName = properties.getProperty("sheetNodeName");
            String sheetDescriptionAttribute = properties.getProperty("sheetDescriptionAttribute");
            if (ExcleConstant.SHEET_NAME_TYPE_REPEAT.equals(sheetNodeType)) {
                if (sheetNodeAttribute == null || sheetNodeAttribute.isEmpty()) {
                    logger.error("请在配置文件中配置作为Sheet页的节点名称");
                }
                if (sheetNodeName == null || sheetNodeName.isEmpty()) {
                    logger.error("请在配置文件中配置作为Sheet页名称的属性名称");
                }
            }
            String rowNodeType = properties.getProperty("rowNodeType", ExcleConstant.ROW_NODE_TYPE_ATTRIBUTE);
            String rowNodeName = properties.getProperty("rowNodeName");
            String rowOffset = properties.getProperty("rowOffset", "0");
            String cellOffset = properties.getProperty("cellOffset", "0");

            String cellNameMapKey = properties.getProperty("cellNameMapKey");
            String cellNameMapValue = properties.getProperty("cellNameMapValue");


            XmlConfiguration.sheetNodeType = sheetNodeType;
            XmlConfiguration.sheetNodeName = sheetNodeName;
            XmlConfiguration.sheetNameAttribute = sheetNodeAttribute;
            XmlConfiguration.sheetDescriptionAttribute = sheetDescriptionAttribute;
            XmlConfiguration.rowNodeName = rowNodeName;
            XmlConfiguration.rowOffset = Integer.parseInt(rowOffset);
            XmlConfiguration.cellOffset = Integer.parseInt(cellOffset);
            XmlConfiguration.rowNodeType = rowNodeType;

            if(cellNameMapKey != null && !cellNameMapKey.isEmpty()){
                Map<String, String> globalCellNameMap = new HashMap<>();
                String[] cellList = cellNameMapKey.split(",");
                String[] cellNameList = cellNameMapValue.split(",");

                for (int i = 0; i<cellList.length; i++) {
                    globalCellNameMap.put(cellList[i],cellNameList[i]);
                }
                XmlConfiguration.attributeNameMap = globalCellNameMap;
            }

        } catch (IOException e) {
            logger.error("读取Xml文件格式配置文件失败,请检查配制文件是否存在！");
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static String getSheetNodeType() {
        return sheetNodeType;
    }

    public static String getSheetNodeName() {
        return sheetNodeName;
    }

    public static String getSheetNameAttribute() {
        return sheetNameAttribute;
    }

    public static String getSheetDescriptionAttribute() {
        return sheetDescriptionAttribute;
    }

    public static String getRowNodeName() {
        return rowNodeName;
    }

    public static String getRowNodeType() {
        return rowNodeType;
    }

    public static int getRowOffset() {
        return rowOffset;
    }

    public static int getCellOffset() {
        return cellOffset;
    }

    public static Map<String, String> getAttributeNameMap() {
        return attributeNameMap;
    }

}
